<!--
 * @Author: yaoling 1871883713@qq.com
 * @Date: 2022-11-27 21:54:42
 * @LastEditors: yaoling 1871883713@qq.com
 * @LastEditTime: 2022-12-19 13:50:37
 * @FilePath: \company-world\src\views\home\components\NavigationBar.vue
 * @Description: 菜单栏
-->
<template>
  <div class="menu">
    <!-- 公司logo -->
    <div class="menu-top-icon">
      <img
        src="https://openim-1309784708.cos.ap-shanghai.myqcloud.com/57fa02ef96c36dadd4e32de306ad7a69.png"
        width="60"
        height="60"
        alt=""
      />
    </div>
    <!-- 菜单 -->
    <div class="menu-container" v-if="menusList && menusList.length">
      <div v-for="(route, index) in menusList" :key="index + route.name" class="menu-item">
        <div class="menu-item-container" v-if="route.is_show">
          <el-tooltip effect="dark" placement="right" :content="route.name">
            <div
              class="icon-label-container"
              @click.stop="menuItemClick(route)"
              :style="{ backgroundColor: route.background_color }"
            >
              <i :class="'iconfont ' + route.icon"></i>
            </div>
          </el-tooltip>
        </div>
      </div>
      <div class="menu-item">
        <div class="menu-item-container" @click="addMenuClick()">
          <el-tooltip effect="dark" placement="right">
            <template #content>
              <p>新增菜单</p>
            </template>
            <div class="icon-label-container border">
              <i class="iconfont iconxinzeng"></i>
            </div>
          </el-tooltip>
        </div>
      </div>
    </div>
    <!-- 头部栏头像 -->
    <section class="Head-portrait-box">
      <img :src="userInfo.avatar || state.defaultAvatar" alt="" class="avatar" />
    </section>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import Bus from '@/utils/bus'
import { Itype } from '@/type/index'
import useCurrentInstance from '@/utils/useCurrentInstance'
const props = defineProps({
  // 侧边菜单栏数据
  menusList: {
    type: Object,
    required: true,
    default: () => {
      return []
    }
  }
})
const emit = defineEmits(['menuItemClick', 'addMenuClick'])
const state = reactive({
  // 默认头像
  defaultAvatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
})
/**
 *
 * @desc: 获取登录用户信息
 */
const { proxy } = useCurrentInstance()
let userInfo: Itype = ref({})
const getUserInfo = () => {
  userInfo.value = proxy.$GetUserInfo()
}
getUserInfo()
/**
 * @desc: 新增菜单
 */
const showDawer = ref(false)
const addMenuClick = () => {
  showDawer.value = !showDawer.value
  emit('addMenuClick', showDawer.value)
}
/**
 * @desc: 获取当前点击菜单数据
 */
const showMenuBar = ref(false)
let currentData = ref({})
const menuItemClick = (data: {}) => {
  showMenuBar.value = true
  currentData.value = data
  sessionStorage.setItem('activeMenu', JSON.stringify(data))
  emit('menuItemClick', data, showMenuBar.value)
}
const handleOpen = (key: string, keyPath: string[]) => {}
const handleClose = (key: string, keyPath: string[]) => {}
onMounted(() => {
  let activeMenu: Itype = ref({})
  activeMenu.value = JSON.parse(sessionStorage.getItem('activeMenu') as string) || ''
  if (activeMenu.value) {
    menuItemClick(activeMenu.value)
  }
})
</script>

<style lang="less" scoped>
.border {
  border: 1.5px solid #816ce7;
}
.menu {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 56px;
  height: 100%;
  padding-top: 8px;
  box-sizing: border-box;
  background-color: #6045e2;
  color: #fff;
  transition: width 1s;
  .Head-portrait-box {
    display: flex;
    justify-content: center;
    padding-bottom: 40%;
    box-sizing: border-box;
    border-radius: 50%;
    cursor: pointer;
    img {
      height: 36px;
      width: 36px;
      border-radius: 8px;
      border-radius: 50%;
      -o-object-fit: cover;
      object-fit: cover;
    }
    .iconfont {
      font-family: PingFangSC, PingFangSC-Medium;
      font-weight: 500;
      font-size: 17px;
      line-height: 24px;
    }
  }
  .menu-top-icon {
    width: 100%;
    height: 20%;
    text-align: center;
    line-height: 60px;
    img {
      width: 100%;
    }
  }
  .menu-container::-webkit-scrollbar {
    display: none; /* Chrome Safari */
  }
  .menu-container {
    flex: 1;
    overflow-y: auto;
    padding: 0 4px;
    box-sizing: border-box;
    /* 隐藏滚动条 */
    scrollbar-width: none; /* firefox */
    -ms-overflow-style: none; /* IE 10+ */
    .menu-item {
      width: 100%;
      margin-top: 20%;
      box-sizing: border-box;
      .menu-item-container {
        display: flex;
        flex-direction: column;
        align-items: center;
        overflow: hidden;
        height: 52px;
        text-align: center;
        i {
          flex: 1;
          line-height: 28px;
        }
        .icon-label-container {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 65%;
          height: 55%;
          border-radius: 4px;
          i {
            flex: 1;
            line-height: 28px;
          }
          span {
            flex: 1;
            overflow: hidden;
            color: #fff;
            line-height: 18px;
            font-size: 12px;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
      }
    }
  }
}
</style>
